package com.sangu.dianxiu.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.R;
import com.sangu.dianxiu.adapter.RepairProjectListLiewAdapter;
import com.sangu.dianxiu.bean.repairtype.RepairProjectListEntity;
import com.sangu.dianxiu.utils.RepairTypeManager;

import java.util.ArrayList;
import java.util.HashMap;

public class RepairProjectActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener2<ListView>{

    private RepairTypeManager manager=new RepairTypeManager();
    private int page;
    private RepairProjectListLiewAdapter repairProjectLvAdapter;
    private ArrayList<RepairProjectListEntity.ItemsEntity> list=new ArrayList<>();
    private int id;
    @ViewInject(R.id.iv_activity_repairproject_back)
    private ImageView ivBack;
    @ViewInject(R.id.ptr_activity_repairproject_list)
    private PullToRefreshListView ptrList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_project);
        ViewUtils.inject(this);
        Intent intent=getIntent();
        id=intent.getIntExtra("id",0);
        initView();

    }

    private void initView() {
        ptrList.setMode(PullToRefreshBase.Mode.PULL_FROM_START);            //设置刷新模式
        ptrList.setOnRefreshListener(this);                                 //设置刷新监听
        repairProjectLvAdapter=new RepairProjectListLiewAdapter(list,this);
        ptrList.setAdapter(repairProjectLvAdapter);
        ptrList.setRefreshing();                                               //设置刚进入此activity的时候就进行加载数据
        ptrList.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(RepairProjectActivity.this,OrderActivity.class);
                intent.putExtra("name",list.get(position-1).getItemName());
                intent.putExtra("projectId",list.get(position-1).getItemId());
                startActivity(intent);                                          //跳转到订单activity
            }
        });
    }

    @OnClick({R.id.iv_activity_repairproject_back})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.iv_activity_repairproject_back:
                finish();
                break;
        }
    }
    private void loadData() {
        HashMap<String,String> map=new HashMap<>();
        map.put("cateId",id+"");
        manager.getRepairProject(map, new ICallBack() {
            @Override
            public void onSuccess(String string) {
                String s=string;
                ptrList.onRefreshComplete();
                RepairProjectListEntity entity= JSONObject.parseObject(string,RepairProjectListEntity.class);
                if(entity.getRc()==0)
                {
                    if(page==1)
                    {
                        list.clear();
                    }
                    list.addAll(entity.getItems());
                    repairProjectLvAdapter.notifyDataSetChanged();
                }
            }
            @Override
            public void onError(VolleyError error) {
            }
        });
    }
    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {            //下拉刷新，
        page=1;
        loadData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {      //上拉加载，没必要上拉加载
        page=1;
        loadData();
    }

    //当销毁activity的时候取消网络请求，减少流量消耗，这也正是volley的优点之一
    @Override
    protected void onDestroy() {
        super.onDestroy();
        manager.requestQueue.cancelAll(RepairTypeManager.class);
    }
}
